package jaoso.framework.dao.hibernate;

import jaoso.framework.dao.BaseDAO;
import jaoso.framework.dao.MyQuery;
import jaoso.framework.dao.RoleDAO;

import jaoso.framework.domain.Account;
import jaoso.framework.domain.Right;
import jaoso.framework.domain.Role;

import jaoso.framework.exception.AccountAlreadyExistException;
import jaoso.framework.exception.DAOException;

import jaoso.framework.util.MyUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;

import java.util.Iterator;
import java.util.Set;

/**
 * @author Administrator
 * @version 0.9.1
 * @since 2004-5-29 Role DAO, persistent Role.
 */
public class RoleDAOImpl implements RoleDAO
{
    /** log */
    private static Log log = LogFactory.getLog(RoleDAOImpl.class);

    /** baseDAO */
    private BaseDAO baseDAO;

    /**
     * Creates a new RoleDAOImpl object.
     */
    public RoleDAOImpl()
    {
        super();
    }

    /**
     * @param dao
     *            dao
     */
    public final void setBaseDAO(final BaseDAO dao)
    {
        baseDAO = dao;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#isExist(java.lang.String)
     */
    public final boolean isExist(final String name)
    {
        boolean flage = false;

        if (MyUtils.isBlank(name))
        {
            return flage;
        }

        try
        {
            // flage = getGroupByDesc(name) != null;
            flage = ((Integer) baseDAO
                    .findEntity("select count(*) from Role a where a.desc='"
                            + name + "'")[0]).intValue() > 0;
        } catch (DAOException e)
        {
            log.error("Role isExist error: ", e);
        }

        return flage;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#isExist(java.lang.String,
     *      java.io.Serializable)
     */
    public final boolean isExist(final String name, final Serializable id)
    {
        boolean flage = false;

        if (MyUtils.isBlank(name) || (id == null))
        {
            return flage;
        }

        try
        {
            /*
             * Role group = getGroupByDesc(name); flage = (group != null) &&
             * !group.getId() .equals(id);
             */
            flage = ((Integer) baseDAO
                    .findEntity("select count(*) from Role a where a.desc='"
                            + name + "' and a.id<>'" + id + "'")[0]).intValue() > 0;
        } catch (DAOException e)
        {
            log.error("Role isExist error: ", e);
        }

        return flage;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#getGroup(java.io.Serializable)
     */
    public final Role getGroup(final Serializable id)
    {
        return (Role) baseDAO.loadEntity(Role.class, id);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#getGroupByName(java.lang.String)
     */
    public final Role getGroupByDesc(final String name)
    {
        if (MyUtils.isBlank(name))
        {
            return null;
        }

        Role[] groups = findGroup("from Role a where a.desc='" + name
                + "' order by a.desc");

        if (MyUtils.isBlank(groups))
        {
            return null;
        }

        return groups[0];
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#createGroup(jaoso.framework.domain.Role)
     */
    public final void createGroup(final Role group)
    {
        baseDAO.saveEntity(group);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#findAllGroup()
     */
    public final Role[] findAllGroup()
    {
        return (Role[]) findGroup("from Role a order by a.desc");
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#findGroup(java.lang.String)
     */
    public final Role[] findGroup(final String query)
    {
        MyQuery myquery = new MyQuery();
        myquery.setQueryString(query);

        return findGroup(myquery);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#findGroup(jaoso.framework.baseDAO.MyQuery)
     */
    public final Role[] findGroup(final MyQuery query)
    {
        Object[] os = baseDAO.findEntity(query);

        if (MyUtils.isBlank(os))
        {
            return null;
        }

        Role[] entitys = new Role[os.length];
        System.arraycopy(os, 0, entitys, 0, os.length);

        return entitys;
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#removeGroup(jaoso.framework.domain.Role)
     */
    public final void removeGroup(final Role group)
    {
        Set accounts = group.getAccounts();
        Set rights = group.getRights();
        Account account;
        Right right;

        if ((accounts != null) && (accounts.size() > 0))
        {
            for (Iterator i = accounts.iterator(); i.hasNext();)
            {
                account = (Account) i.next();
                account.setGroup(null);
                baseDAO.updateEntity(account);
            }

            group.setAccounts(null);
        }

        if ((rights != null) && (rights.size() > 0))
        {
            for (Iterator i = rights.iterator(); i.hasNext();)
            {
                right = (Right) i.next();
                right.getGroups().remove(group);
                baseDAO.updateEntity(right);
            }

            group.setRights(null);
        }

        baseDAO.removeEntity(group);
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#removeGroup(java.io.Serializable)
     */
    public final void removeGroup(final Serializable id)
    {
        if (id == null)
        {
            throw new DAOException("id can't be null");
        }

        removeGroup(getGroup(id));
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#removeGroup(java.io.Serializable[])
     */
    public final void removeGroup(final Serializable[] ids) throws AccountAlreadyExistException
    {
        if (ids == null)
        {
            throw new DAOException("id can't be null");
        }

        for (int i = 0, n = ids.length; i < n; i++)
        {
            Role role = getGroup(ids[i]);
            if (role.getAccounts()!=null && role.getAccounts().size() != 0)
            {
                throw new AccountAlreadyExistException();    
            }
            removeGroup(role);
        }
    }

    /**
     * (non-Javadoc)
     * 
     * @see jaoso.framework.baseDAO.RoleDAO#updateGroup(jaoso.framework.domain.Role)
     */
    public final void updateGroup(final Role group)
    {
        baseDAO.updateEntity(group);
    }
}